package org.grow.cooldown.ScoreModule.entity;

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
@Data
@Document
public class Score {
    @Id
    private String ScoreId;
    private String StudentId;
    private Double math = 0d;
    private Double python = 0d;
    private Double java = 0d;
    private String operatorId;
    private LocalDate scoreCT;

    private Boolean frozen= false;
    //保持数据完整性 准确性  入库前的验证工作 繁琐而无趣
    public Double getAverage(){
        return (math+python+java)/3;
    }
    public void valid(){
        if (math<0 || math >150) throw new ScoreException("math 0-150");
        if (python <0 || python >120) throw new ScoreException("python 0-120");
        if (java <0 || java >120) throw new ScoreException("java 0-120");
        if (operatorId == null || operatorId.equals(""))
            throw  new ScoreException("operator missed");
        if (StringUtils.isEmpty(StudentId)) throw  new RuntimeException("student missed");
    }

    public static class ScoreException extends RuntimeException{
        public ScoreException(String message){
            super(message);
        }
        private int code;

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }
    }
}
